package top.zhangjianyong.tools.service.api.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import top.zhangjianyong.tools.entity.poet.Poet;
import top.zhangjianyong.tools.function.TwoParamConsumer;
import top.zhangjianyong.tools.service.api.PoetService;
import top.zhangjianyong.tools.service.mybatis.IPoetService;
import top.zhangjianyong.tools.utils.ApiResponse;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;

/**
 * @author ： zhangjianyong
 * @date ： 2023/1/19 11:16 AM
 */
@Service
public class PoetServiceImpl implements PoetService {

    @Autowired
    private IPoetService iPoetService;

    @Value("${poet.path}")
    private String PATH;

    @Override
    public ApiResponse shi() {
        String path = PATH + "/json";
        File dir = new File(path);
        File[] files = dir.listFiles();

        TwoParamConsumer<File, String> consumer = (file, type) -> {
            try {
                String read = IoUtil.read(new FileReader(file));
                JSONArray array = JSONObject.parseArray(read);
                System.out.println(array);
                for (int i = 0; i < array.size(); i++) {
                    JSONObject json = array.getJSONObject(i);
                    Poet poet = creatShi(json, type);
                    if (!iPoetService.existByUuid(poet)) {
                        iPoetService.save(poet);
                    }
                }
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            }
        };

        Stream.of(files).filter(f -> f.getName().contains("poet.song"))
                .forEach(file -> consumer.accept(file, "宋诗"));
        Stream.of(files).filter(f -> f.getName().contains("poet.tang"))
                .forEach(file -> consumer.accept(file, "唐诗"));


        return ApiResponse.success();
    }


    public Poet creatShi(JSONObject json, String type) {
        String paragraphs = json.getString("paragraphs");
        String author = json.getString("author");
        String tags = json.getString("tags");
        String title = json.getString("title");
        String id = json.getString("id");

        Poet poet = new Poet();
        poet.setAuthor(author);
        poet.setParagraphs(paragraphs);
        poet.setTags(tags);
        poet.setTitle(title);
        poet.setType(type);
        poet.setUuid(id);
        return poet;
    }

    @Override
    public ApiResponse ci() {
        String path = PATH + "/ci";
        File dir = new File(path);
        File[] files = dir.listFiles();
        Stream.of(files).filter(file -> file.getName().contains("ci.song"))
                .forEach(file -> {
                    try {
                        String read = IoUtil.read(new FileReader(file));
                        JSONArray array = JSONObject.parseArray(read);
                        for (int i = 0; i < array.size(); i++) {
                            JSONObject json = array.getJSONObject(i);
                            Poet poet = createCi(json);
                            if (!iPoetService.existByParagraphs(poet)) {
                                iPoetService.save(poet);
                            }
                        }
                    } catch (FileNotFoundException e) {
                        throw new RuntimeException(e);
                    }

                });
        return ApiResponse.success();
    }

    private Poet createCi(JSONObject json) {
        Poet poet = new Poet();
        String author = json.getString("author");
        String paragraphs = json.getString("paragraphs");
        String rhythmic = json.getString("rhythmic");
        String tags = json.getString("tags");
        poet.setAuthor(author);
        poet.setParagraphs(paragraphs);
        poet.setTitle(rhythmic);
        poet.setTags(tags);
        poet.setTags("宋词");
        return poet;

    }
}
